package com.tsmti.webservice.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * @Package：com.tsmti.webservice.util
 * @Class：ClassUtils
 * @Description： TODO
 * @Author：king
 * @Date：Created in 2018/9/3 10:16
 * @Company:
 * @Version：
 * @Modified By:
 */
public class ClassUtils extends PropertyUtilsBean {
    private static Logger log = Logger.getLogger(ClassUtils.class);

    /**
     * 数据库数据转化成实体类
     * @param rs
     * @param clazz
     * @return
     */
    public static Object setBeanProperty(ResultSet rs, Class clazz) {
        Object ins = null;
        try {
            ins = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            try {

                while (rs.next()){
                    for (Field field:fields) {
                        String name = field.getName();
                        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(ins, name);
                        if (pd == null) {
                            continue;
                        }
                        String propertyTypeName = pd.getPropertyType().getName();
                        if (propertyTypeName.equals(int.class.getName())
                                || propertyTypeName.equals(Integer.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getInt(name));
                        } else if (propertyTypeName.equals(long.class.getName())
                                || propertyTypeName.equals(Long.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getLong(name));
                        } else if (propertyTypeName.equals(double.class.getName())
                                || propertyTypeName.equals(Double.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getDouble(name));
                        } else if (propertyTypeName.equals(float.class.getName())
                                || propertyTypeName.equals(Float.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getFloat(name));
                        } else if (propertyTypeName.equals(byte.class.getName())
                                || propertyTypeName.equals(Byte.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getByte(name));
                        } else if (propertyTypeName.equals(short.class.getName())
                                || propertyTypeName.equals(Short.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getShort(name));
                        } else if (propertyTypeName.equals(char[].class.getName())
                                || propertyTypeName.equals(Character[].class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getCharacterStream(name));
                        } else if (propertyTypeName.equals(boolean.class.getName())
                                || propertyTypeName.equals(Boolean.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getBoolean(name));
                        } else if (propertyTypeName.equals(String.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getString(name));
                        } else if (propertyTypeName.equals(Date.class.getName())) {
                            pd.getWriteMethod().invoke(ins, rs.getDate(name));
                        }
                    }
                }
            } catch (InvocationTargetException | NoSuchMethodException e) {
                log.error(e.getCause().getMessage());
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getCause().getMessage());
            e.printStackTrace();
        }
        return ins;
    }

    /**
     * 对象拷贝
     * 数据对象空值不拷贝到目标对象
     *
     * @param databean
     * @param tobean
     * @throws NoSuchMethodException
     * copy
     */
    public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
    {
        PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(databean);
        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();
            if ("class".equals(name)) {
                // No point in trying to set an object's class
                continue;
            }
            if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(databean, name);
                    if(value!=null){
                        getInstance().setSimpleProperty(tobean, name, value);
                    }
                }
                catch (java.lang.IllegalArgumentException ie) {

                }
                catch (Exception e) {

                }

            }
        }
    }
}
